#ifndef __IntTreat_C
#define __IntTreat_C

#include "IntTreat.h" 
extern volatile uint8_t UpboardUpdate;

// 全局时钟
#if RTC_ON == 0 || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
void Tmr_CLOCK_IRQHandler(void){	  
    if(TMR_ReadIntFlag(Tmr_CLOCK_Id, Tmr_Default_INT_FLG) == SET){ 
        #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
			    if(CP_CarD1_TmrBusy == 1){
						  #if CarD1_Protect > 0
						  // 二极管检测处理
						  if(PWM_Data.pwmDuty > 50 && PWM_Data.pwmDuty < 950){
                  if(carD1_get_level() == 0){
										  if(CarD1Param.CarD1_MarkNum < 65532) CarD1Param.CarD1_MarkNum++; //1ms1次; 最大就是65秒
									} 
									#if CarD1_WholeLife_EN > 0
									else {
										  CarD1Param.CarD1_MarkNum = 0;
									} 									
									#endif																
					    } 
						  #endif							
					}else if(CP_CarD1_TmrBusy == 2){
						  // CP的ADC检测处理
						  #if filter_CP_coeff_bit  > 0 
              cp_data.ADC_Data = cp_data.ADC_Data == 0 ? ADC_Buffer.ADC_Item.ADCValue_CP : (cp_data.ADC_Data + ((ADC_Buffer.ADC_Item.ADCValue_CP >> filter_CP_coeff_bit) - (cp_data.ADC_Data >> filter_CP_coeff_bit)));
              //printf("%d \r\n",((ADC_Buffer.ADC_Item.ADCValue_CP - cp_data.ADC_Data) >> filter_CP_coeff_bit));						
						  #else
						  cp_data.ADC_Data = ADC_Buffer.ADC_Item.ADCValue_CP; 
              //printf(" %d ",ADC_Buffer.ADC_Item.ADCValue_CP);						
						  #endif						  
					}          			    
        #else			
			    system_time++;	
        #endif 			
        TMR_ClearIntFlag(Tmr_CLOCK_Id, Tmr_Default_INT_FLG);
        #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay	
					TMR_Disable(Tmr_CLOCK_Id);	//关闭定时器
					CP_CarD1_TmrBusy = 0;
        #endif					
    }
}
#endif

#ifndef UsingFreeRTOS
// 流程函数时钟：SysTick_Handler优先级值最大，需修改优先级。
void SysTick_Handler(void){ 
    // 漏电检测：
	  #if RCD_Detect == RCD_Detect_POLL
	  LeakDetect();	  
	  #endif
	  #if Using_UART_LCD == LCD_0P42_IIC	  
		// oled重置操作
		if(oled_reset_step_tm > 0){
	      #if RTC_ON > 0
				getTimestampRTC();
				#endif
			  if((uint16_t)(system_time & 0x000000000000ffff) - oled_reset_step_tm > oled_reset_step_delay){
						oled_reset_step_run();
				}
		}
	  #endif
		
		// 互感器自检或校验初始化操作：
		#if (RCD_TYPE_B > 0) && (testSelfLeak > 0)
		if(leakData.leak_test_init_step > 0 && leakData.leakSelfTest_start_tm > 0 && leakData.leak_test_init_step < Leak_Step_max){			  
			  leak_test_step_run();
		}		
		#endif
		
		// 蜂鸣器鸣叫
		#if beep_type == beep_APM
		if(beep.beep_step > 0 && beep.beep_step < beep.beep_times * 2){
			  beep_step_run();
		}
		#endif
		
		// Lcd蜂鸣器鸣叫
		#if beep_type == beep_LCD && Using_UART_LCD == LCD_4P3_UART
		if(beep.beep_step > 0 && beep.beep_step <= beep.beep_times){ 
			  lcd_beep_step_run(); 
		} 
		#endif 
}
#endif

#ifndef UsingFreeRTOS
// 执行任务时钟；10ms
void Tmr_Task_IRQHandler(void){	  
	  if(TMR_ReadIntFlag(Tmr_Task_Id, Tmr_Default_INT_FLG) == SET){	
			  task_tms++;
			  task_scheduling();
			  TMR_ClearIntFlag(Tmr_Task_Id, Tmr_Default_INT_FLG);
		}	  
}

// 任务调度
void task_scheduling(){
	  // 喂狗：			  
		IWDT_Refresh();	
	
	  // 获取系统时间
	  #if RTC_ON > 0 
		getTimestampRTC(); 
		#endif
	
		// 开机任务
		if(mTask_SW & mTask_ledStartUp_bit){
				mTaskLedStartUp();
		}
		
		// 停充任务
		#if proactive_chgStop_EN > 0 || PlugCharge_And_Resv
		if(mTask_SW & mTask_initChgStop_bit){
				if(stopChargeTmr > 0){
						stopChargeTmr--;
				} else {
					  // 按键停充：需要延时
					  PWM_Duty_Set(DUTY_CYCLE_stop);
						Rly_off();
						chg_stage = CHG_STOP;
						stopChargeTmr = 0;
						mTask_SW = mTask_SW & (~mTask_initChgStop_bit);
				}
		}	
		#endif			  
		
		if(mTask_SW & mTask_ledBlink_bit){ 
				// 闪灯任务					  
				mTaskLedBlink(); 
		}
		
		#ifdef LedCtrl_Rolling				
		if(mTask_SW & mTask_ledRoll_bit){ 
				// 灯滚动任务					  
				mTaskLedRollBlink(); 
		}	
		#endif
		
		// 自检：1000ms查一次状态;				
		if(task_tms % self_check_ticks == 17){ 
				self_check();
		}
		
		#if PE_ON > 0
		if(PE_EN > 0 && system_started > 0){
			  #if PE_method == PE_method_ADC 
			  PE_VoltRest = (UI_Data.U_RMS - 90) * 4; 
        #endif			
				PEDetect();
		}				
		#endif 
						
		// key输入检测：50ms检测一次；
		if(task_tms % EintFreq == 0 && system_started > 0){
				#if Key1  > 0 			  
			  keyDetect();
				#endif
						
				#if PinPick_RatedI > 0 
				PinPick_Detect();
				#endif
		}	
		
		// 读取接地状态	
		#if PE_ON > 0
		if(PE_EN > 0 && task_tms % PE_Read_ticks == 34 && system_started > 0){
				get_PE_state();	// 7KW调试测试不需要接地				  
		}	
		#endif				
			
		
		// 读取CC状态:1秒检测1次;
		#if CC_Detect_type > 0
		if(task_tms % CC_Dectect_ticks == 87 && system_started > 0){ 
				CC_Detect();		    
		}
		#endif
								
		// 读取继电器粘连状态
		#if RlyAdh_Detect_type > 0 
		if(task_tms % RlyAdh_Dectect_ticks == 79){
				if(cp_data.value_cope_id != CP_6){
						RlyAdh_Detect();
				}		  
		}				
		#endif
						
		#if (UI_Meter_Method > Meter_SelfADC)
		// 读取电表记录芯片数据
		if(task_tms % read_UI_intv == 49 && system_started > 0){
				readUIValueFromGuage();
		}
		#elif (UI_Meter_Method == Meter_SelfADC)
		if(task_tms % read_UI_intv == 49 && system_started > 0){
			  readUIValue();
		}		
		#endif		
		// 计算电量
		#if (Get_QOE_Method == Meter_SelfADC)
		if(task_tms % compuQOETick == 99 && system_started > 0){
				UI_Data.QOE = UI_Data.QOE + UI_Data.Papt * K_KWh2WS;
		}				
		#endif
		
		#if meter485_ON > 0
		if(task_tms % read_UI_intv == 11 && system_started > 0){ 
				readUIValueFromMeter485();
			  if(system_time - mt485RdTm > mt485OffLineTime){
					  mt485_Data.meterReady = 0;
				}
		}    		
		#endif
		
		// 数据检测：100ms执行一次;
		if(task_tms % check_state_ticks == 7 && system_started > 0){
				// 获取温度数据的原始值：	
				getTMP_BoardIn_R();			  
				#if tmpr_nums > 1 && PinPick_RatedI == 0
				getTMP_Ext1_R();
				#endif
				#if tmpr_nums > 2  
				getTMP_Ext2_R();
				#endif					
				// 温度校对
			  #if tmpr_EN > 0
				get_tmpr_state();
			  #endif
				// U I 校对
				if(UI_Data.meterReady > 0){
						get_I_state();
						get_U_state();
				}
		}
		
		// 获取温度值：1秒1次;
		if(task_tms % get_tmprValue_ticks == 67 && system_started > 0){
				// 获取温度数据
				getTMP_BoardIn();	
				#if tmpr_nums > 1
				getTMP_Ext1();
				#endif			      
				#if tmpr_nums > 2  
				getTMP_Ext2();
				#endif	 
				// 获取CP电压
				getCPValue();
				//printf("==== %f   %f  %d  %d ====\r\n",TMP_Ext1,TMP_Ext2,TMP_Ext1_R,TMP_Ext2_R);
				#if (UI_Meter_Method == Meter_SelfADC) 
				getTMP_U_Avail(); 
				getTMP_I_Avail();	
			  printf(" %.3f  %u  %.3f  %u  rt = %u  adj = %u  ov = %u  st = %u \r\n",UI_Data.U_RMS,uiValues.U_value,UI_Data.I_RMS,uiValues.I_value, I_State_Flag.I_rated_value,I_State_Flag.I_adjust,I_State_Flag.I_over_current,I_State_Flag.I_short_circuit);
				getPowerValue();
				#endif
			  // 温度滤波
			  #if filter_Tmpr_coeff_bit > 0
			  // 滤波：温度保存1位小数
				TMP_show_int = (int)(TMP_SHOW * 10);
			  TMP_show_lb = TMP_show_lb == 0 ? TMP_show_int : (TMP_show_lb + ((TMP_show_int >> filter_Tmpr_coeff_bit) - (TMP_show_lb >> filter_Tmpr_coeff_bit)));
			  #endif	
		}
									
		// 电量保存频率
		#if (GET_K_Actv != 0)  || (Get_QOE_Method == Meter_SelfADC) 
		if(task_tms % QOE_save_ticks == 0 && cp_data.value_cope_id == CP_6){ 
				writeData[FWD_QOE_idx].fltData = UI_Data.QOE; 
				flash_write();
		}
		#endif
										
		// 保护恢复任务：
		if(task_tms % check_state_ticks == 5 && system_started > 0){ 
				protect_recover(); 
		}
						
		#if Has_RlyPin_Maintaining > 0
		if(Rly_startup_status != 2 && Relay_Chg_tm > 0 && Rly_status == 1){
				#if RTC_ON > 0
				getTimestampRTC();
				#endif				
				if(system_time - Relay_Chg_tm > Rly_PinStartup_Timeout){
						// 关闭继电器的启动引脚
						Rly_release_startup_Pin();
						#ifdef SYS_DEBUG
						printf("Rly_release_startup_Pin  \r\n");	
						#endif							
				}
		}				
		#endif
		
		// 刷卡
		#if card_type_sel > 0
		if(task_tms % cardDetectFreq == 93 && RC522_ready > 0 && system_started > 0){
				if(isCardBusy <= 0){
						isCardBusy = 1;
						// 设置操作的块id：
						cardOper_blockId = 2;
						card_task_read_start();
				}
				isCardBusy--;
		}	
		#endif

    // 指纹识别业务和串口屏业务分开执行
		#if finger_sensor_type == Finger_HLK_ZW101_EINT  
		if(mTask_SW & mTask_initStartUp_bit){ 
				// 开机后的指纹识别的初始化任务：启动指纹生物识别
				mTaskFingerInitStartUp(); 
		} else {
		#endif					
				#if Using_UART_LCD == LCD_4P3_UART 	        
				if(task_tms % refreshLCDDataFreq == refreshLCDData_timeSeg && system_started > 0){ 
            if(lcdParam.lcd_popup_time > 0) lcdParam.lcd_popup_time--;
            #if meter485_ON > 0 					
            if(mt485_Data.meterReady > 0){
							  UI_Data_Show = &mt485_Data;
						}else{
							  UI_Data_Show = &UI_Data;
						} 
            #endif		

						// 刷新屏幕数据						  
						display_write_variate(DATA_TEMPEARTURE,(uint16_t)(TMP_show_lb)); 
						display_write_variate(DATA_CHG_VOLTAGE,(uint16_t)(LCD4P3_SHOW_U_RMS));           						
						display_write_variate(DATA_PRACTICAL_CURRENT,(uint16_t)((LCD4P3_SHOW_I_RMS < 0.2 ? 0 : LCD4P3_SHOW_I_RMS) * 10));	 
						display_write_variate(DATA_RATED_CURRENT,(uint16_t)(I_State_Flag.I_rated_valuef));    
						display_write_variate(DATA_ELECTRIC_QUANTITY,(uint16_t)((LCD4P3_SHOW_QOE - LCD4P3_SHOW_QOE_ChgS) / QOE_Dec_Places_1)); 					  
						#if PowerSupplyPhase == 3
						display_write_variate(DATA2_CHG_VOLTAGE,(uint16_t)(LCD4P3_SHOW_UB_RMS));
						display_write_variate(DATA2_PRACTICAL_CURRENT,(uint16_t)(LCD4P3_SHOW_IB_RMS * 10));
						display_write_variate(DATA3_CHG_VOLTAGE,(uint16_t)(LCD4P3_SHOW_UC_RMS));
						display_write_variate(DATA3_PRACTICAL_CURRENT,(uint16_t)(LCD4P3_SHOW_IC_RMS * 10));
            display_write_variate(DATA_CHGR_POWER,(uint16_t)(LCD4P3_SHOW_Papt	* 0.01));	//显示屏单位=0.1KW=100W；						
						#endif
						
						// 倒计时显示
						#if SEND_RTC_EN > 0
						display_write_arr_variate(TMRTXT_COUNTDOWN,lcdParam.countdownStart,1);   
						lcdParam.countdownStart[0]--;
						if(lcdParam.countdownStart[0] < 0) lcdParam.countdownStart[0] = 0;
						#endif
						
						// 计时显示 
						#if SEND_RTC_EN > 0
						//tmrTimeAdd(lcdParam.timer_recoder,3);
						getTmrShow(lcdParam.timer_recoder,3);
						lcdParam.RTC_data[6] = lcdParam.timer_recoder[0];	
						lcdParam.RTC_data[5] = lcdParam.timer_recoder[1]; 	
						lcdParam.RTC_data[4] = lcdParam.timer_recoder[2];
						display_write_rtc(TMRTXT_TIMER_SHOW); 
            #endif
						
						// 页面显示
						if(system_time - led_startUp_TmRecode < 3500){
								// LCD自检页面
								lcdParam.lcd_show_step = lcd_step_self_check;	
						}else{
								if(faultCode == 0){
										if(cp_data.value_cope_id == CP_12 && Rly_status == 0){
												lcdParam.lcd_show_step = lcd_step_wait_plug;
										} else if(cp_data.value_cope_id == CP_9 && chg_selfCheck_complt != chg_selfCheck_SUM && Rly_status == 0){
												lcdParam.lcd_show_step = lcd_step_safe_check;
										} else if((cp_data.value_cope_id == CP_9 || cp_data.value_cope_id == CP_6) && chg_selfCheck_complt == chg_selfCheck_SUM && Rly_status == 0){
												if(chg_start_tm == 0){
													  lcdParam.lcd_show_step = lcd_step_wait_chg;
												} else {
														if(lcdParam.lcd_show_step != lcd_step_chg_full && lcdParam.lcd_show_step != lcd_step_plug_out) {
															  lcdParam.lcd_show_step = lcd_step_EV_pause;
														} else {
															  lcdParam.lcd_show_step = lcd_step_plug_out;
														}
												}
										} else if(cp_data.value_cope_id == CP_6 && chg_selfCheck_complt == chg_selfCheck_SUM && Rly_status == 1){
											  if(chg_stage == CHG_FULL){
														lcdParam.lcd_show_step = lcd_step_chg_full;
												}else{
														lcdParam.lcd_show_step = lcd_step_chging;
												}
										} 
								}else{
										lcdParam.lcd_show_step = lcd_step_chg_error;
								}
						}
						if(lcdParam.lcd_show_step > 0){									
								update_page();
						}	
						#if meter485_ON > 0
            //printf(" %f  %f  %f  %f  %f  %f  %f  %f  %f  %f  %f %f \r\n", \
				                    mt485_Data.U_RMS,mt485_Data.UB_RMS,mt485_Data.UC_RMS,mt485_Data.I_RMS,mt485_Data.IB_RMS,mt485_Data.IC_RMS,\
				                    mt485_Data.P_actv,mt485_Data.PB_actv,mt485_Data.PC_actv,mt485_Data.PT_actv,mt485_Data.Papt,mt485_Data.QOE);	
						//printf(" %f  %f  %f  %f  %f  %u \r\n", \
				                    mt485_Data.U_RMS,mt485_Data.I_RMS,mt485_Data.P_actv,mt485_Data.Papt,mt485_Data.QOE,rdMeter485Idx);	
            #endif						
				}	 			
				#endif
		#if finger_sensor_type == Finger_HLK_ZW101_EINT	
		}				
		// 启动指纹识别： 				
		#if Using_UART_LCD == LCD_4P3_UART
		if(fingerCtrl.finger_event_occ > 0 && system_started > 0 && (task_tms % refreshLCDDataFreq) != refreshLCDData_timeSeg){
		#else
		if(fingerCtrl.finger_event_occ > 0 && system_started > 0){
		#endif		
        // 搜索指纹			
				finger_sendCmd(PS_AutoIdentify);
				fingerCtrl.finger_event_occ = 0;
		}
		#endif				
			
		// 指纹识别-轮询
		#if finger_sensor_type == Finger_HLK_ZW101_POLL 		
		if(task_tms % fingerDetectFreq == 109 && system_started > 0){
				#ifdef SYS_DEBUG
				//printf("  fingerDetectFreq fingerCtrl.finger_busy = %d  %d  \r\n",fingerCtrl.finger_busy,fingerCtrl.finger_step);	
				#endif 
				if(fingerCtrl.finger_busy == 0){ 
						if(fingerCtrl.finger_step == 1){ 
								finger_sendCmd(PS_ReadSysPara);  
						} else if(fingerCtrl.finger_step == 2){
								// 搜索指纹
								finger_sendCmd(PS_AutoIdentify);  
						} else if(fingerCtrl.finger_step == 3){
								finger_sendCmd(PS_AutoEnroll); 
						}				  
						fingerCtrl.finger_busy = 10;
						if(fingerCtrl.fingerCMD == PS_ReadSysPara || fingerCtrl.fingerCMD == PS_AutoIdentify){
								fingerCtrl.finger_busy = 1;
						} 				  
				}
				fingerCtrl.finger_busy--;
		}
		#endif
		#if finger_sensor_type > 0
		  if(externalIntv > 0) externalIntv--;
		#endif
				
		#if Using_UART_LCD == LCD_0P42_IIC
    // 重新初始化
		#ifndef UsingFreeRTOS
		if(OledResetTm == 1){ 
				ssd1306_init();	
			  OledResetTm++;
		}		
		#endif
		// 刷新屏幕
		if(task_tms % refreshOledFreq == 97 && oled_reset_step > 1){
        if(OledResetTm < 4){
					  OledResetTm++;
				}else{
					  getShowData();
				} 
		}
		#endif
				
		// 保存功率曲线; 10分钟保存一次
		#if CURVE_ON > 0
		if(task_tms % save_curve_ticks == 17 && Rly_status == 1){
				curvTemp.occTime  = system_time / 1000;
				curvTemp.chgI     = 0;
				curvTemp.chgPwr   = 0;
				curvTemp.chgTmpr  = 0;
				#if UI_Meter_Method == Meter_GuageIC_RN8302B 
				curvTemp.chgBI     = 0;
				curvTemp.chgBPwr   = 0;
				curvTemp.chgCI     = 0;
				curvTemp.chgCPwr   = 0;					  
				#endif					
				if(curv_data_append(curvTemp)){
						writeData[FWD_CurvSize_idx].intData = (uint32_t)Curv_Saved_Sum;
						// 数据保存
						flash_write();
						flash_curv_write();
				}	
		}
		#endif // #if CURVE_ON > 0
		
		// 获取CP状态: 50ms一次;		
		if(task_tms % CP_state_ticks == 2){
			  if(cp_data.ADC_Data > cp_0V_low){
						get_cp_state();
				}
		}
		
		// 预约充电相关
		#if RESV_CHG_ON  > 0 || PlugCharge_And_Resv
//		if(resv_data.resv_start_point > 0 && resv_data.resv_time[0] > 1 && task_tms % resv_ticks == resv_timeSeg && resv_data.resv_time_idx < resv_numbs){
//				if(resv_data.resv_time[resv_data.resv_time_idx] > 0 && (system_time - (resv_data.resv_time[resv_data.resv_time_idx] * 60000 + resv_data.resv_start_point)) > 0){
//					  if((system_time - (resv_data.resv_time[resv_data.resv_time_idx] * 60000 + resv_data.resv_start_point)) <= resv_ticks){
//							  // 已到预约时间;
//								if(resv_data.resv_time_idx % 2 == 1){
//										// 充电启动：
//										if(startChgExternalReason.chargeControl > 0){
//												startChgExternalReason.chargeControl_bit.reason_resv = 0;	
//												// 提前关闭PWM波
//												PWM_Duty_Set(DUTY_CYCLE_stop);
//		                    #if proactive_chgStop_EN > 0
//													//延时10s关机
//													stopChargeTmr = EvStopChgTimer;
//													// 进入关机流程
//													mTask_SW = mTask_SW | mTask_initChgStop_bit;
//													#if Using_UART_LCD == LCD_4P3_UART
//													// 屏幕显示关机
//													lcdParam.lcd_popup_time = 10;  
//													lcdParam.lcd_show_popup = lcd_popup_delay_stop;
//													#endif
//												#else //#if proactive_chgStop_EN > 0
//													Rly_off();
//												#endif //#if proactive_chgStop_EN > 0
//												#ifdef SYS_DEBUG
//												printf("RESV_CHG stop charge!!!!!   \r\n");	
//												#endif
//										}									  
//								}else{
//										// 充电停止
//										if(startChgExternalReason.chargeControl == 0){
//												startChgExternalReason.chargeControl_bit.reason_resv = 1;
//												// 刷新并重新启动充电
//												main_update = 1;									  
//												#ifdef SYS_DEBUG
//												printf("RESV_CHG charge start!!!!!   \r\n");	
//												#endif
//										}
//								}	
//						}else{
//							  // 过了预约时间：
//							  resv_data.resv_time_idx++; //期待下次预约;
//						}
//				} else {
//						// 未到预约时间
//						
//						
//				}	
//        // 	重复预约：
//        if(resv_data.resv_time_idx == (resv_numbs - 1) && resv_data.resv_week.week > 0){
//					  resv_data.resv_time_idx = 0; 					  
//					  // 明天周几的计算
//						resv_data.resv_week_idx = ((system_time / 1000) % 604800)  / 86400 + 5;  //时间戳起始点是周四
//						resv_data.resv_week_idx = resv_data.resv_week_idx > 7 ? resv_data.resv_week_idx - 7 : resv_data.resv_week_idx;
//						// 判断明天是否有预约：
//						if(resv_data.resv_week.week & (0x01 << (resv_data.resv_week_idx - 1))){
//								// 已经预约，可重复执行：
//								resv_data.resv_start_point = resv_data.resv_start_point + 86400000 * ((system_time - resv_data.resv_start_point) / 86400000);
//						}	  
//				}				
//		}		

		if(task_tms % resv_ticks == resv_timeSeg && resv_data.resv_start_point > 0){
					if(resv_data.resv_time[resv_data.resv_time_idx] > 1)
					{
						if(system_time > resv_data.resv_time[resv_data.resv_time_idx] * 60000 + resv_data.resv_start_point)
						{
							if(resv_data.resv_time_idx % 2 == 1){
										// 停止充电：
//										if(startChgExternalReason.chargeControl > 0){
												startChgExternalReason.chargeControl_bit.reason_resv = 0;	
												// 提前关闭PWM波
												PWM_Duty_Set(DUTY_CYCLE_stop);	
												//延时10s关机
												stopChargeTmr = EvStopChgTimer;
												// 进入关机流程
												mTask_SW = mTask_SW | mTask_initChgStop_bit;
												#if Using_UART_LCD == LCD_4P3_UART
												// 屏幕显示关机
												lcdParam.lcd_popup_time = 10;  
												lcdParam.lcd_show_popup = lcd_popup_delay_stop;
												#endif
												#ifdef SYS_DEBUG
												printf("RESV_CHG stop charge!!!!!   \r\n");	
												#endif
//										}									  
								}else{
										// 开始充电
//										if(startChgExternalReason.chargeControl == 0){
												startChgExternalReason.chargeControl_bit.reason_resv = 1;
												// 刷新并重新启动充电
												main_update = 1;									  
												#ifdef SYS_DEBUG
												printf("RESV_CHG charge start!!!!!   \r\n");	
												#endif
//										}
								}
								resv_data.resv_start_point = resv_data.resv_start_point + resv_data.resv_time[resv_data.resv_time_idx] * 60000;
								resv_data.resv_time_idx++;
						}
					}
					else
					{
						resv_data.resv_time_idx++;
					}
					
					if(resv_data.resv_time_idx >= 2)
					{
						resv_data.resv_start_point = 0;
						resv_data.resv_time_idx = 0;
						resv_data.resv_time[0] = 0;
						resv_data.resv_time[1] = 0;
					}
			}
		#endif
		
		//blewifi模块
		#if BleWifi_type == BleWifi_Ai_WB2
		// 轮流查询
		if(task_tms % bleWifi_Reset_ticks == 77 && system_started > 0 && (system_time - system_started) > 3000){
        if(bleWiFi_Data.bw_reset_step > 0 && bleWiFi_Data.bw_reset_step <= bw_reset_max){
			      bleWifi_restart();
            bleWiFi_Data.bw_reset_step++;
		    }
				
		}
		
		// 轮流设置
		if(task_tms % bleWifi_ticks == 237 && system_started > 0){
			  if(strLenMy(bleWiFi_Data.bw_sta_name,bw_name_size) > 2 && bleWiFi_Data.bw_reset_step > bw_reset_max && bleWiFi_Data.bw_wifi_conn == 0){
					  wifi_conn();
					  bleWiFi_Data.bw_wifi_conn++;
				}
	
		}
		
		// 发送数据到服务器：
		if(task_tms % bleWifi_ticks == 377 && system_started > 0){
		    if((bleWiFi_Data.bw_wifiMode == 1 || bleWiFi_Data.bw_wifiMode == 3) && bleWiFi_Data.bw_ble_conn == 0 && bleWiFi_Data.bw_wifi_conn > 0) {
					  // 发送数据到服务器：
					  chgr_snapshort.snapshot_data.CSS_CP_id = cp_data.value_cope_id;
					  chgr_snapshort.snapshot_data.CSS_PWM_Duty = PWM_Data.pwmDuty;
					  chgr_snapshort.snapshot_data.CSS_relay_status = Rly_status;
					  chgr_snapshort.snapshot_data.CSS_reted = (uint16_t)(I_State_Flag.I_rated_valuef);
					  chgr_snapshort.snapshot_data.CSS_Tmpr_Board = (uint16_t)(TMP_BoardIn * 10);  //单位=0.1℃
					  chgr_snapshort.snapshot_data.CSS_Tmpr_chgr  = (uint16_t)(TMP_Ext1 * 10);
					  chgr_snapshort.snapshot_data.CSS_Tmpr_plugr = (uint16_t)(TMP_Ext2 * 10);					  
					  chgr_snapshort.snapshot_data.CSS_U = (uint16_t)(LCD4P3_SHOW_U_RMS);
					  chgr_snapshort.snapshot_data.CSS_I = (uint16_t)(LCD4P3_SHOW_I_RMS * 10);
					  chgr_snapshort.snapshot_data.CSS_Powr = (uint16_t)(LCD4P3_SHOW_Papt	* 0.01);//显示屏单位=0.1KW=100W；
					  chgr_snapshort.snapshot_data.CSS_QOE = (uint16_t)((LCD4P3_SHOW_QOE - LCD4P3_SHOW_QOE_ChgS) / QOE_Dec_Places_1);	
            #if PowerSupplyPhase == 3					
					  chgr_snapshort.snapshot_data.CSS_UB = (uint16_t)(LCD4P3_SHOW_UB_RMS);
					  chgr_snapshort.snapshot_data.CSS_IB = (uint16_t)(LCD4P3_SHOW_IB_RMS * 10);
					  chgr_snapshort.snapshot_data.CSS_UC = (uint16_t)(LCD4P3_SHOW_UC_RMS);
					  chgr_snapshort.snapshot_data.CSS_IC = (uint16_t)(LCD4P3_SHOW_IC_RMS * 10);
					  #endif
					  chgr_snapshort.snapshot_data.CSS_FaultCode = mTask_Fault;
					  appendArr(chgr_snapshort.snapshot_data.CSS_devName,Chgr_Name,0,8);
					  // 数据加密
					  //encode_SM4(chgrSnapshotLen,srv_pwd,chgr_snapshort.arr,encryptionArr);
					  xw_endecode(chgr_snapshort.arr,chgrSnapshotLen,srv_pwd,16,encryptionArr);
												 
					  // base64编码						
						base64_encode(encryptionArr,chgrSnapshotSM4Len,base64Arr);
											
						
					  // 提交数据
					  blewifi_httpReq();
				}
		}
		#endif
		
		// 主策略一直运行；
		if(task_tms % mTaskMainFreq == 1){
				mTaskLed_strategy();
			  mTaskCharge_strategy();
		}
		
		// 错误策略也一直执行；
		if(task_tms % mTaskFaultFreq == 3){
				mTaskFault_strategy();
		}
		
}

#endif //#ifndef UsingFreeRTOS

// ADC数据读取中断:  
#if UI_Meter_Method == Meter_SelfADC || CP_Mode == CP_Mode_sta
void TmrAdc_IRQHandler(void){
		if(TMR_ReadIntFlag(Tmr_ADC_Id, Tmr_Default_INT_FLG) == SET){
			  //if(DMA_ReadStatusFlag(DMA1_FLAG_TC1) == SET){
            #if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
					  if(adc_adjust % adc_ticks == 0){
            #endif				  
            // 处理数据					
            if(ADC_read_times < UI_Datas_Len){							                  						
							  #if UI_Meter_Method == Meter_SelfADC							  
							  UI_Value[ADC_read_times][ADC_ChgU] = ADC_Buffer.ADC_Item.ADCValue_ChgU;
					      UI_Value[ADC_read_times][ADC_ChgI] = ADC_Buffer.ADC_Item.ADCValue_ChgI;
                //UI_Value_time[ADC_read_times] = system_time;							
							  #endif
                #if CP_Mode == CP_Mode_sta						
                CP_Datas[ADC_read_times] = ADC_Buffer.ADC_Item.ADCValue_CP;
							  #endif
                #if volt_zero_crossing	> 0	 					  
							  #if UI_Meter_Method == Meter_SelfADC
							  // 过零检测:							  
							  if(rly_on_mark == 1){
									  //if(ADC_Value[ADC_ChgU] > volt_zero_strt && ADC_Value[ADC_ChgU] < volt_zero_end){
							      if(ADC_Buffer.ADC_Item.ADCValue_ChgU > volt_zero_strt && ADC_Buffer.ADC_Item.ADCValue_ChgU < volt_zero_end){
												//下降沿触发
												if(UI_Value[ADC_read_times][ADC_ChgU] < UI_Value[ADC_read_times - 1][ADC_ChgU]){
														rly_on_mark = 0;
														Rly_on();													  
												}
										}
								}		
                #endif
							  #endif	
                ADC_read_times++;								
						}	else {
                #if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
							    TMR_DisableInterrupt(Tmr_ADC_Id, TMR_INT_UPDATE);
	              #else
							    TMR_Disable(Tmr_ADC_Id);
	              #endif	
							  //printf(" %lld ",system_time);
						}
						//DMA_ClearStatusFlag(DMA1_FLAG_TC1);						
				#if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
				}
				#endif
				
				#if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
				adc_adjust++;
				#endif			  	
        TMR_ClearIntFlag(Tmr_ADC_Id, Tmr_Default_INT_FLG);			  
    }
}
#endif

// I2C-DMA中断
#if Using_UART_LCD == LCD_0P42_IIC || Upboard_IIC == 1
volatile uint8_t IIC_TransmitOk = 1;
void I2C_Tx_DMAChanel_IRQHandler(void){
	  if(DMA_ReadIntFlag(I2C_Tx_DMA_INT_TC) == SET){
			  uint8_t ii = I2C_TimeOut;
			  while ((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) && ii--){ }	
			  I2C_EnableGenerateStop(IIC_ID);             // 必须加结束条件;
				DMA_ClearIntFlag(I2C_Tx_DMA_INT_TC);
				IIC_TransmitOk = 1;
		}
}
#endif

void Tmr_PWM_IRQHandler(void){	  
	  #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT	|| CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay 
    if(TMR_ReadIntFlag(Tmr_PWM_Id, PWM_CC_INT_FLAG) == SET){
			  Tmr_PWM_TO_LowLevel_ACTION();			  
				TMR_ClearIntFlag(Tmr_PWM_Id, PWM_CC_INT_FLAG);					        
    }
	  #else		
    if(TMR_ReadIntFlag(Tmr_PWM_Id, CarD1_INT_FLAG) == SET){
			  Tmr_PWM_TO_LowLevel_ACTION();        
				TMR_ClearIntFlag(Tmr_PWM_Id, CarD1_INT_FLAG);	
    }
		#endif		
		#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CC50CC950
		// 读取CP等数据;
		if(TMR_ReadIntFlag(Tmr_PWM_Id, ADC_INT_FLAG) == SET){
			 	Tmr_PWM_TO_HighLevel_ACTION();	
        TMR_ClearIntFlag(Tmr_PWM_Id, ADC_INT_FLAG);
    }
		#endif
}

#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
void PWM_TMR_UP_IRQHandler(){
	  if(TMR_ReadIntFlag(Tmr_PWM_Id, PWM_TMR_UP_FLAG) == SET){
			  Tmr_PWM_TO_HighLevel_ACTION();  
			  TMR_ClearIntFlag(Tmr_PWM_Id, PWM_TMR_UP_FLAG);
		}	  
}
#endif

// PWM转向低电平后,需要做的事;
void Tmr_PWM_TO_LowLevel_ACTION(){ 
	  // 读取数据：1ms一次; 
		#if CarD1_Protect > 0 && (CarD1_Detect_Tyte == CarD1_Detect_OCC_Trg)
	  if(CarD1Param.CarD1_TestStart > 0){
			  #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT
			  #ifdef AT32F403AVGT7
			  delaynus(1); // 5us = 0.5%的占空比
			  #else
			  delaynus(1); // 5us = 0.5%的占空比
			  #endif
			  #endif			  
			  #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
			    if(CP_CarD1_TmrBusy == 0){
              TMR_ConfigAutoreload(Tmr_CLOCK_Id, TmrPeriod_CarD1Delay);               					  
						  TMR_Enable(Tmr_CLOCK_Id);	//启动定时器						  
              CP_CarD1_TmrBusy = 1;						
					}			    
			  #else			  
					if(carD1_get_level() == 0 && PWM_Data.pwmDuty > 50 && PWM_Data.pwmDuty < 950){ 
							if(CarD1Param.CarD1_MarkNum < 65532) CarD1Param.CarD1_MarkNum++; //1ms1次; 最大就是65秒
					} 
				#endif
		}	  
    #endif
}

// PWM转向高电平后,需要做的事: 1ms 执行一次;
void Tmr_PWM_TO_HighLevel_ACTION(){
	   if(DMA_ReadStatusFlag(DMA1_FLAG_TC1) == SET){
				// CP数据计算 
				#if (UI_Meter_Method == Meter_SelfADC) && (CP_Mode == CP_Mode_sta)
				readUIValue();	
				#endif					
				#if CP_Mode == CP_Mode_sta
				if(cp_data.ADC_Data == 0){
						//cp_data.ADC_Data = ADC_Value[ADC_CP];
			      cp_data.ADC_Data = ADC_Buffer.ADC_Item.ADCValue_CP; 
				} 
        //get_cp_state();
				//cp_data.ADC_Data = 0;					
				#elif CP_Mode == CP_Mode_tick	
        #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT
					#ifdef AT32F403AVGT7 
					delaynus(1);
					#else
					delaynus(1);			 
					#endif 
			  #endif
			  #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
			    if(CP_CarD1_TmrBusy == 0){
						  CP_CarD1_TmrBusy = 2;	
              TMR_ConfigAutoreload(Tmr_CLOCK_Id, TmrPeriod_ADCDelay);						
						  TMR_Enable(Tmr_CLOCK_Id);	//启动定时器              					
					}			    
			  #else
					#if filter_CP_coeff_bit  > 0 
					cp_data.ADC_Data = cp_data.ADC_Data == 0 ? ADC_Buffer.ADC_Item.ADCValue_CP : (cp_data.ADC_Data + ((ADC_Buffer.ADC_Item.ADCValue_CP >> filter_CP_coeff_bit) - (cp_data.ADC_Data >> filter_CP_coeff_bit)));	
					#else
					cp_data.ADC_Data = ADC_Buffer.ADC_Item.ADCValue_CP;	
					#endif
        #endif				
				#endif				
				DMA_ClearStatusFlag(DMA1_FLAG_TC1);
		}		
}

#if UI_Meter_Method == Meter_SelfADC
void readUIValue(){
    uint32_t  U_max;
		uint32_t  I_max;	
    #if (GET_UI_METHOD_ID == UI_METHOD_RMS)
    uint32_t  U_sum = 0;
		uint32_t  I_sum = 0; 
 	  #endif
	  #if order_of_filter > 1
	  uint8_t  lvidx = 0;
	  #endif
	  // 读取UI数据
		if(ADC_read_times >= UI_Datas_Len){
			  #ifdef SYS_DEBUG
			  //printf("  readUIValue111 %d \n\r",ADC_read_times);
			  #endif
			  uint8_t i = 0;				 			  
			  #if (GET_UI_METHOD_ID == UI_METHOD_MAX)
            U_max = UI_Value[0][0];
				    I_max = UI_Value[0][1];
        #elif (GET_UI_METHOD_ID == UI_METHOD_RMS)			     
            uiValues.U_value = 0;
				    uiValues.I_value = 0;			      
        #endif 
			
			  #if CP_Mode == CP_Mode_sta
					  cp_data.ADC_Data = CP_Datas[0];
			  #endif
        			
        for(i = 0;i < UI_Datas_Len; i++){
					  #if CP_Mode == CP_Mode_sta
					  // 取CP最大值;
					  if(cp_data.ADC_Data < CP_Datas[i]){
							  cp_data.ADC_Data = CP_Datas[i];
						}    
					  #endif
					  #if (GET_UI_METHOD_ID == UI_METHOD_MAX)
						if(U_max < UI_Value[i][0]){
								U_max = UI_Value[i][0];
								#if (GET_K_Actv != 0)
								U_idx = i;
								#endif
						}
						if(I_max < UI_Value[i][1]){
								I_max = UI_Value[i][1];
								#if (GET_K_Actv != 0)
								I_idx = i;
								#endif
						}
						#elif (GET_UI_METHOD_ID == UI_METHOD_RMS)
						#if (GET_K_Actv != 0)	            					
						if(U_max < UI_Value[i][0]){								
							  U_max = UI_Value[i][0];
							  U_idx = i;
						}
						if(I_max < UI_Value[i][1]){								
							  I_max = UI_Value[i][1];
							  I_idx = i;
						}						
						#endif						 
            U_sum = U_sum + UI_Value[i][0] * UI_Value[i][0];
            I_sum = I_sum + UI_Value[i][1] * UI_Value[i][1];
						#endif
            //printf(" %d ",UI_Value[i][1]);
				}
        //printf("   ============== \r\n ");				
        // 判据滤波
				#if (GET_UI_METHOD_ID == UI_METHOD_MAX)
					#if filter_UI_coeff_bit  > 0				    
							uiValues.U_value_lb[0] = uiValues.U_value_lb[0] == 0 ? U_max : (uiValues.U_value_lb[0] + ((U_max >> filter_UI_coeff_bit) - (uiValues.U_value_lb[0] >> filter_UI_coeff_bit)));
							uiValues.I_value_lb[0] = uiValues.I_value_lb[0] == 0 ? I_max : (uiValues.I_value_lb[0] + ((I_max >> filter_UI_coeff_bit) - (uiValues.I_value_lb[0] >> filter_UI_coeff_bit)));
							for(lvidx = 1;lvidx < order_of_filter; lvidx++){							  
									uiValues.U_value_lb[lvidx] = uiValues.U_value_lb[lvidx] == 0 ? uiValues.U_value_lb[lvidx-1] : (uiValues.U_value_lb[lvidx] + ((uiValues.U_value_lb[lvidx-1] >> filter_UI_coeff_bit) - (uiValues.U_value_lb[lvidx] >> filter_UI_coeff_bit)));
									uiValues.I_value_lb[lvidx] = uiValues.I_value_lb[lvidx] == 0 ? uiValues.I_value_lb[lvidx-1] : (uiValues.I_value_lb[lvidx] + ((uiValues.I_value_lb[lvidx-1] >> filter_UI_coeff_bit) - (uiValues.I_value_lb[lvidx] >> filter_UI_coeff_bit)));								  
							}	
							uiValues.U_value = uiValues.U_value_lb[lvidx-1];
							uiValues.I_value = uiValues.I_value_lb[lvidx-1];			    
					#else
							uiValues.U_value = U_max;
							uiValues.I_value = I_max;		
											
					#endif
				#elif (GET_UI_METHOD_ID == UI_METHOD_RMS)
				  #if filter_UI_coeff_bit  > 0
				    uiValues.U_value_lb[0] = uiValues.U_value_lb[0] == 0 ? U_sum : (uiValues.U_value_lb[0] + ((U_sum >> filter_UI_coeff_bit) - (uiValues.U_value_lb[0] >> filter_UI_coeff_bit)));
						uiValues.I_value_lb[0] = uiValues.I_value_lb[0] == 0 ? I_sum : (uiValues.I_value_lb[0] + ((I_sum >> filter_UI_coeff_bit) - (uiValues.I_value_lb[0] >> filter_UI_coeff_bit)));
						for(lvidx = 1;lvidx < order_of_filter; lvidx++){							  
							  uiValues.U_value_lb[lvidx] = uiValues.U_value_lb[lvidx] == 0 ? uiValues.U_value_lb[lvidx-1] : (uiValues.U_value_lb[lvidx] + ((uiValues.U_value_lb[lvidx-1] >> filter_UI_coeff_bit) - (uiValues.U_value_lb[lvidx] >> filter_UI_coeff_bit)));
						    uiValues.I_value_lb[lvidx] = uiValues.I_value_lb[lvidx] == 0 ? uiValues.I_value_lb[lvidx-1] : (uiValues.I_value_lb[lvidx] + ((uiValues.I_value_lb[lvidx-1] >> filter_UI_coeff_bit) - (uiValues.I_value_lb[lvidx] >> filter_UI_coeff_bit)));								  
						}	
						uiValues.U_value = uiValues.U_value_lb[lvidx-1];
						uiValues.I_value = uiValues.I_value_lb[lvidx-1];
					#else
						uiValues.U_value = U_sum;
						uiValues.I_value = I_sum;							
					#endif				  
				#endif
				// 显示滤波：
        #if filter_UIShow_coeff_bit  > 0	
					uiValues.Ulb_value[0] = uiValues.Ulb_value[0] == 0 ? uiValues.U_value : (uiValues.Ulb_value[0] + ((uiValues.U_value >> filter_UIShow_coeff_bit) - (uiValues.Ulb_value[0] >> filter_UIShow_coeff_bit)));
					uiValues.Ilb_value[0] = uiValues.Ilb_value[0] == 0 ? uiValues.I_value : (uiValues.Ilb_value[0] + ((uiValues.I_value >> filter_UIShow_coeff_bit) - (uiValues.Ilb_value[0] >> filter_UIShow_coeff_bit)));
				  for(lvidx = 1;lvidx < order_of_filter; lvidx++){
						  uiValues.Ulb_value[lvidx] = uiValues.Ulb_value[lvidx] == 0 ? uiValues.Ulb_value[lvidx-1] : (uiValues.Ulb_value[lvidx] + ((uiValues.Ulb_value[lvidx-1] >> filter_UIShow_coeff_bit) - (uiValues.Ulb_value[lvidx] >> filter_UIShow_coeff_bit)));
					    uiValues.Ilb_value[lvidx] = uiValues.Ilb_value[lvidx] == 0 ? uiValues.Ilb_value[lvidx-1] : (uiValues.Ilb_value[lvidx] + ((uiValues.Ilb_value[lvidx-1] >> filter_UIShow_coeff_bit) - (uiValues.Ilb_value[lvidx] >> filter_UIShow_coeff_bit)));
          }
				#endif		
				
        //printf(" %u \r\n",uiValues.I_value);				
				ADC_read_times = 0;
				#if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
					TMR_EnableInterrupt(Tmr_ADC_Id, TMR_INT_UPDATE);
          //printf(" %lld ",system_time);					
	      #else
				  TMR_Enable(Tmr_ADC_Id);
	      #endif				
		} 
}

// 获取功率值：
void getPowerValue(){
    #if (GET_K_Actv != 0)
		UI_Data.PF_pow = (float)cos((U_idx - I_idx) * 0.01 * pi);
		if(UI_Data.PF_pow < PF_nomal){
				UI_Data.PF_pow = PF_nomal;
		}
		UI_Data.Papt = UI_Data.I_RMS * UI_Data.U_RMS;
		UI_Data.P_actv = UI_Data.Papt * fabs(UI_Data.PF_pow);        
		// UI_Data.QOE = UI_Data.QOE + UI_Data.P_actv * K_KWh2WS;
		#ifdef SYS_DEBUG
		// printf("  K = %.2f, pt = %.2f, pact = %.2f, q = %.4f \n\r",PF_pow,Pt,P_actv,QOE);
		#endif //SYS_DEBUG
		#endif		
}

#endif  // #if UI_Meter_Method == Meter_SelfADC

#if using_uart1 > 0
// 串口1接收数据：uart1
void UART1_IRQn_Fun(void){
    if(USART_ReadIntFlag(UART1_Id, USART_INT_RXBNE)){ 			  
			  // idx = 0判定
			  if(uart1Ctrl.uart_idx >= rxBuf1_size) uart1Ctrl.uart_idx = 0;
			  uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx] = (uint8_t)USART_RxData(UART1_Id);
			  if(uart1Ctrl.uart_idx > 0){
					  if(uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx-1] == uart1_header_H && uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx] == uart1_header_L){
							  uart1Ctrl.uart_idx = 2;
							  uart1Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart1Ctrl.uartRxDatas,rxBuf1_size);
							  uart1Ctrl.uartRxDatas[0] = uart1_header_H;  
							  uart1Ctrl.uartRxDatas[1] = uart1_header_L;
							  uart1Ctrl.recv_ctrl = 1;							  
							  #if defined uart1_header2_H && defined uart1_header2_H
							  uart1Ctrl.end_type = 1;
							  #endif
							  return;
						}
						#if defined uart1_header2_H && defined uart1_header2_H
						if(uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx-1] == uart1_header2_H && uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx] == uart1_header2_L){
							  uart1Ctrl.uart_idx = 2;
							  uart1Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart1Ctrl.uartRxDatas,rxBuf1_size);	
                uart1Ctrl.uartRxDatas[0] = uart1_header2_H;  
							  uart1Ctrl.uartRxDatas[1] = uart1_header2_L;					  
							  uart1Ctrl.recv_ctrl = 1;
							  uart1Ctrl.end_type = 2;							
							  return;
						}
						#endif
				} 
				#if defined uart1_header2_H && defined uart1_header2_H
				if(uart1Ctrl.recv_ctrl > 0 && uart1Ctrl.end_type == 1){
				#else
				if(uart1Ctrl.recv_ctrl > 0){
				#endif
					  // 结束条件
						#if len1_bit_num > 0	
							#if len1_bit_num > 1
							if(uart1Ctrl.uart_idx == len1_bit + 1){
									uart1Ctrl.datLen = uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx - 1] * 256 + uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx];
					        uart1Ctrl.datLen = (uart1Ctrl.datLen + len1_bit + len1_bit_num  + (check1_out_Len) - 1);
							}
							#else
							if(uart1Ctrl.uart_idx == len1_bit){
									uart1Ctrl.datLen = uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx];
								  uart1Ctrl.datLen = (uart1Ctrl.datLen + len1_bit + len1_bit_num  + (check1_out_Len) - 1);
							}				
							#endif				
							if(uart1Ctrl.uart_idx == uart1Ctrl.datLen && uart1Ctrl.datLen > 0){
									treatUart1Info();
							}
						#else
							#ifdef uart1_End_H 
							if(uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx - 1] == uart1_End_H && uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx] == uart1_End_L){
									treatUart1Info();
							}
							#endif
						#endif
				}	

        #if defined uart1_header2_H && defined uart1_header2_H
				if(uart1Ctrl.recv_ctrl > 0 && uart1Ctrl.end_type == 2){				
            // 第二种参数
            #if len1_bit_num2 > 0					
							#if len1_bit_num2 > 1
							if(uart1Ctrl.uart_idx == len1_bit_2 + 1){
									uart1Ctrl.datLen = (uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx - 1] << 8) + uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx];
									uart1Ctrl.datLen = (uart1Ctrl.datLen + len1_bit_2 + len1_bit_num2  + (check1_out_Len2) - 1);
							}
							#else
							if(uart1Ctrl.uart_idx == len1_bit_2){
									uart1Ctrl.datLen = uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx];
									uart1Ctrl.datLen = (uart1Ctrl.datLen + len1_bit_2 + len1_bit_num2 + (check1_out_Len2) - 1);//长度不包含检验位，就是-1；包含就是 +1。
							}				
							#endif	
							if(uart1Ctrl.uart_idx == uart1Ctrl.datLen && uart1Ctrl.datLen > 0){
									treatUart1Info();
							}
						#else
							// 结束符号结束：
							#ifdef uart1_End2_H 
							if(uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx - 1] == uart1_End2_H && uart1Ctrl.uartRxDatas[uart1Ctrl.uart_idx] == uart1_End2_L){
									treatUart1Info(); 
							}
							#endif  
						#endif							
				}	
        #endif   				
				
				uart1Ctrl.uart_idx++;			  
    }
}
#endif


#if using_uart2 > 0
// 串口3接收数据：uart1
void UART2_IRQn_Fun(void){	
    if(USART_ReadIntFlag(UART2_Id, USART_INT_RXBNE)){         
			  // idx = 0判定
			  if(uart2Ctrl.uart_idx >= rxBuf2_size) uart2Ctrl.uart_idx = 0;
			  uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx] = (uint8_t)USART_RxData(UART2_Id);
			  if(uart2Ctrl.uart_idx > 0){
					  if(uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx-1] == uart2_header_H && uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx] == uart2_header_L){
							  uart2Ctrl.uart_idx = 2;
							  uart2Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart2Ctrl.uartRxDatas,rxBuf2_size);	
                uart2Ctrl.uartRxDatas[0] = uart2_header_H;
							  uart2Ctrl.uartRxDatas[1] = uart2_header_L;					  
							  uart2Ctrl.recv_ctrl = 1;
							  return;
						}
				}
				if(uart2Ctrl.recv_ctrl > 0){
					  // 结束条件
						#if len2_bit_num > 0	
							#if len2_bit_num > 1
							if(uart2Ctrl.uart_idx == len2_bit + 1){
									uart2Ctrl.datLen = (uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx - 1] << 8) + uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx];
									uart2Ctrl.datLen = (uart2Ctrl.datLen + len2_bit + len2_bit_num + check2_out_Len - 1);
							}
							#else
							if(uart2Ctrl.uart_idx == len2_bit){
									uart2Ctrl.datLen = uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx];
								  uart2Ctrl.datLen = (uart2Ctrl.datLen + len2_bit + len2_bit_num + check2_out_Len - 1);
							}				
							#endif	
							if(uart2Ctrl.uart_idx == uart2Ctrl.datLen && uart2Ctrl.datLen > 0){
									treatUart2Info();
							}
						#else
							#ifdef uart2_End_H 
							if(uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx - 1] == uart2_End_H && uart2Ctrl.uartRxDatas[uart2Ctrl.uart_idx] == uart2_End_L){
									treatUart2Info(); 
							}
							#endif  
						#endif	
				}		
			  uart2Ctrl.uart_idx++;
    }
}
#endif


#if using_uart3 > 0
// 串口3接收数据：
void UART3_IRQn_Fun(void){
    if(USART_ReadIntFlag(UART3_Id, USART_INT_RXBNE)){
			  // idx = 0判定
			  if(uart3Ctrl.uart_idx >= rxBuf3_size) uart3Ctrl.uart_idx = 0;
			  uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx] = (uint8_t)USART_RxData(UART3_Id);	 
			  if(uart3Ctrl.uart_idx > 0){
					  if(uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx-1] == uart3_header_H && uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx] == uart3_header_L){
							  uart3Ctrl.uart_idx = 0;
							  uart3Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart3Ctrl.uartRxDatas,rxBuf3_size);
							  uart3Ctrl.recv_ctrl = 1;
							  return;
						} 
				} 
				if(uart3Ctrl.recv_ctrl > 0){					  
					  // 结束条件
						#if len3_bit_num > 0	
						  #if len3_bit_num > 1
							if(uart3Ctrl.uart_idx == len3_bit - 1){
									uart3Ctrl.datLen = (uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx - 1] << 8) + uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx];
					        uart3Ctrl.datLen = (uart3Ctrl.datLen + len3_bit + len3_bit_num - 3);
							}
							#else
							if(uart3Ctrl.uart_idx == len3_bit - 2){
									uart3Ctrl.datLen = uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx];
					        uart3Ctrl.datLen = (uart3Ctrl.datLen + len3_bit + len3_bit_num - 3);
							}				
							#endif				
							if(uart3Ctrl.uart_idx == uart3Ctrl.datLen && uart3Ctrl.datLen > 0){
									treatUart3Info();
							}
						#else
							#ifdef uart3_End_H 
							if(uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx - 1] == uart3_End_H && uart3Ctrl.uartRxDatas[uart3Ctrl.uart_idx] == uart3_End_L){
									treatUart3Info();
							}
							#endif
						#endif
				}				
			  uart3Ctrl.uart_idx++;
    }
}
#endif


#if using_uart4 > 0
// 串口4接收数据：uart1
void UART4_IRQn_Fun(void){
    if(USART_ReadIntFlag(UART4_Id, USART_INT_RXBNE)){
        // idx = 0 不能溢出				  
			  if(uart4Ctrl.uart_idx >= rxBuf4_size) uart4Ctrl.uart_idx = 0; 			
        uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx] = (uint8_t)USART_RxData(UART4_Id); // 接收数据			  
			  if(uart4Ctrl.uart_idx > 0){
					  // 判断是否为数据头
					  if(uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx-1] == uart4_header_H && uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx] == uart4_header_L){               							
							  uart4Ctrl.uart_idx = 0;
							  uart4Ctrl.datLen   = 0;
							  arrAllIs('\0',uart4Ctrl.uartRxDatas,rxBuf4_size);
							  uart4Ctrl.recv_ctrl = 1; // 此后接收的数据有效，且可以处理。
							  return;
						}
				}				
				// 数据接收控制：开头接收或接收标志被置位				
				if(uart4Ctrl.recv_ctrl > 0){
            // 结束条件
						#if len4_bit_num > 0	
							#if len4_bit_num > 1   //长度占2位字符
							if(uart4Ctrl.uart_idx == len4_bit - 1){ //6
									uart4Ctrl.datLen = (uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx - 1] << 8) + uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx];
									uart4Ctrl.datLen = (uart4Ctrl.datLen + len4_bit + len4_bit_num - 3); // 结束的长度;
							}
							#else    //长度占1位字符        
							if(uart4Ctrl.uart_idx == len4_bit - 2){
									uart4Ctrl.datLen = uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx];
							    uart4Ctrl.datLen = (uart4Ctrl.datLen + len4_bit + len4_bit_num - 3); // 结束的长度;
							}				
							#endif              						
							if(uart4Ctrl.uart_idx == uart4Ctrl.datLen && uart4Ctrl.datLen > 0){
									treatUart4Info();
							}
						#else
							#ifdef uart4_End_H 
							if(uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx - 1] == uart4_End_H && uart4Ctrl.uartRxDatas[uart4Ctrl.uart_idx] == uart4_End_L){
									treatUart4Info();
							}
							#endif
						#endif
				}
				uart4Ctrl.uart_idx++;	
    }
}
#endif

#if using_uart5 > 0
// 串口5接收数据：uart1
void UART5_IRQn_Fun(void){
    if(USART_ReadIntFlag(UART5_Id, USART_INT_RXBNE)){
			  // idx = 0判定
			  if(uart5Ctrl.uart_idx >= rxBuf5_size) uart5Ctrl.uart_idx = 0;
			  uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx] = (uint8_t)USART_RxData(UART5_Id);
			  printf("%c",uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx]);
			  if(uart5Ctrl.uart_idx > 0){
					  if(uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx-1] == uart5_header_H && uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx] == uart5_header_L){
							  uart5Ctrl.uart_idx = 2;
							  uart5Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart5Ctrl.uartRxDatas,rxBuf5_size);	
                uart5Ctrl.uartRxDatas[0] = uart5_header_H;  
							  uart5Ctrl.uartRxDatas[1] = uart5_header_L;					  
							  uart5Ctrl.recv_ctrl = 1;
							  #if defined uart5_header2_H && defined uart5_header2_H
							  uart5Ctrl.end_type = 1;
							  #endif
							  return;
						}
						#if defined uart5_header2_H && defined uart5_header2_H
						if(uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx-1] == uart5_header2_H && uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx] == uart5_header2_L){
							  uart5Ctrl.uart_idx = 2;
							  uart5Ctrl.datLen   = 0;
							  arrAllIs(0x00,uart5Ctrl.uartRxDatas,rxBuf5_size);	
                uart5Ctrl.uartRxDatas[0] = uart5_header2_H;  
							  uart5Ctrl.uartRxDatas[1] = uart5_header2_L;					  
							  uart5Ctrl.recv_ctrl = 1;
							  uart5Ctrl.end_type = 2;							
							  return;
						}
						#endif
						
				}				
				#if defined uart5_header2_H && defined uart5_header2_H
				if(uart5Ctrl.recv_ctrl > 0 && uart5Ctrl.end_type == 1){
				#else
				if(uart5Ctrl.recv_ctrl > 0){
				#endif
					  // 结束条件
            // 长度结束
            #if len5_bit_num > 0					
							#if len5_bit_num > 1
							if(uart5Ctrl.uart_idx == len5_bit + 1){
									uart5Ctrl.datLen = (uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx - 1] << 8) + uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx];
									uart5Ctrl.datLen = (uart5Ctrl.datLen + len5_bit + len5_bit_num  + check5_out_Len - 1);
							}
							#else
							if(uart5Ctrl.uart_idx == len5_bit){
									uart5Ctrl.datLen = uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx];
									uart5Ctrl.datLen = (uart5Ctrl.datLen + len5_bit + len5_bit_num + check5_out_Len - 1);//长度不包含检验位，就是-1；包含就是 +1。
							}				
							#endif	
							if(uart5Ctrl.uart_idx == uart5Ctrl.datLen && uart5Ctrl.datLen > 0){
									treatUart5Info();
							}
						#else
							// 结束符号结束：
							#ifdef uart5_End_H 
							if(uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx - 1] == uart5_End_H && uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx] == uart5_End_L){
									treatUart5Info(); 
							}
							#endif  
						#endif
				}
				#if defined uart5_header2_H && defined uart5_header2_H
				if(uart5Ctrl.recv_ctrl > 0 && uart5Ctrl.end_type == 2){				
            // 第二种参数
            #if len5_bit_num2 > 0					
							#if len5_bit_num2 > 1
							if(uart5Ctrl.uart_idx == len5_bit_2 + 1){
									uart5Ctrl.datLen = (uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx - 1] << 8) + uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx];
									uart5Ctrl.datLen = (uart5Ctrl.datLen + len5_bit_2 + len5_bit_num2  + (check5_out_Len2) - 1);
							}
							#else
							if(uart5Ctrl.uart_idx == len5_bit_2){
									uart5Ctrl.datLen = uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx];
									uart5Ctrl.datLen = (uart5Ctrl.datLen + len5_bit_2 + len5_bit_num2 + (check5_out_Len2) - 1);//长度不包含检验位，就是-1；包含就是 +1。
							}				
							#endif	
							if(uart5Ctrl.uart_idx == uart5Ctrl.datLen && uart5Ctrl.datLen > 0){
									treatUart5Info();
							}
						#else
							// 结束符号结束：
							#ifdef uart5_End2_H 
							if(uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx - 1] == uart5_End2_H && uart5Ctrl.uartRxDatas[uart5Ctrl.uart_idx] == uart5_End2_L){
									treatUart5Info(); 
							}
							#endif  
						#endif							
				}	
        #endif   				
			  uart5Ctrl.uart_idx++;
    }
}
#endif


#if PinPick_RatedI > 0
void PinPick_Detect(){
	  if(PinPick_get_level()){
			  // 是否第一次转变为1
			  if(PinPick_state != 1 && chg_start_tm == 0){	
					  #if I_rated_count > 1 
								I_rated_idx = pinLevel_H_indx; 
								PWM_Data.ratedDuty = DUTY_CYCLE_List[I_rated_idx];
                #if meter485_ON > 0
                I_State_Flag.I_rated_value2 = I_rated_List2[I_rated_idx];
                #endif
                I_State_Flag.I_rated_value  = I_rated_List[I_rated_idx];
								I_State_Flag.I_rated_valuef = I_rated_Listf[I_rated_idx]; 
						#endif
						get_I_rated_value();  
						main_update++; 
						#ifdef SYS_DEBUG 
						// printf("PinPick_Detect111  %d  \r\n",PinPick_state);	
						#endif	
				}
				PinPick_state = 1;
		} else {
			  if(PinPick_state != 2 && chg_start_tm == 0){
					  #if I_rated_count > 1
								I_rated_idx = pinLevel_L_indx;
								PWM_Data.ratedDuty = DUTY_CYCLE_List[I_rated_idx];
								#if meter485_ON > 0
                I_State_Flag.I_rated_value2 = I_rated_List2[I_rated_idx];
                #endif
                I_State_Flag.I_rated_value  = I_rated_List[I_rated_idx];
								I_State_Flag.I_rated_valuef = I_rated_Listf[I_rated_idx];						
						#endif	
            get_I_rated_value(); 						
						main_update++;
						#ifdef SYS_DEBUG
						//printf("PinPick_Detect222  %d  \r\n",PinPick_state);	
						#endif		
				}
			  PinPick_state = 2;
		}
		#ifdef SYS_DEBUG
		//printf("PinPick_Detect000  %d  \r\n",PinPick_state);	
		#endif	
}
#endif

#if Key1  > 0
// 第一个按键的输入检测：
void keyDetect(){
	  if(key_get_level() != key1_level){
			  // 记录变化值：
		    key1_level = key_get_level();
			  #if Key_SelfLock == 0
			  // 按键1状态变化了:
			  if(key_get_level() == key1InitLevel){
					  // 高电平:	按键抬起					  
					  // 判断长按
						key1_param.keyChangeUpTms = system_time;
					  #if Key_longCtl > 0
					  #if RTC_ON > 0
						getTimestampRTC();
						#endif
					
					#if key1_Ctrl_id == KeyCtrl_UpBoard
						if(key1_param.keyState == -3)
						{
							key1_param.keyState = 0;
							key1_param.keyDownTms = 0;  
							key1_param.keyUpTms   = 0; 
							return;
						}
					#endif
					
//					  if(key1_param.keyDownTms > 0 && system_time - key1_param.keyDownTms > Key_longHold){  
//							  //长按被触发了; 
//								key1_param.keyState   = -1;
//								#if key1_Ctrl_id == KeyCtrl_UpBoard
//								mEventUpKeyTreat();	 
//								#else
//								mEventKeyTreat();
//								#endif 	  
//								key1_param.keyDownTms = 0;  
//								key1_param.keyUpTms   = 0; 
//								return;
//						}
					  #endif
            // 判断连按
            #if Key_doubleCtl > 0
						key1_param.keyState++;
					  #if RTC_ON > 0
						getTimestampRTC();
						#endif
            key1_param.keyUpTms = system_time;				  
						#endif            
						#if Key_doubleCtl == 0
						key1_param.keyState = 1;  
						#if key1_Ctrl_id == KeyCtrl_UpBoard
						mEventUpKeyTreat();	 
						#else
						mEventKeyTreat();
						#endif
						#endif
				} else {
					  // 低电平: 按键按下
					  #if Key_longCtl > 0
					  #if RTC_ON > 0
						getTimestampRTC();
						#endif
					  key1_param.keyDownTms = system_time;
					  #endif
				}
				#endif
				
				// 按键事件执行：
				#if Key_SelfLock > 0
				// 自锁按键
				key1_param.keyState = key1_level; 
				mEventKeyTreat();
				#endif
		} else { 		
				#if Key_SelfLock == 0
				#if Key_doubleCtl > 0
				// 检测连按：抬起执行
			  #if RTC_ON > 0
				getTimestampRTC();
				#endif
				if(key_get_level() == key1InitLevel && key1_param.keyUpTms > 0 && system_time - key1_param.keyUpTms > Key_doubleInTV){
						mEventKeyTreat();		
						key1_param.keyUpTms = 0;
            #if Key_longCtl > 0
					  key1_param.keyDownTms = 0;
            #endif					
				}
				#endif
				if(key_get_level() == key1InitLevel && key1_param.keyChangeUpTms > 0 && system_time - key1_param.keyChangeUpTms > Key_NoOperateInTV)
				{
					key1_param.keyChangeUpTms = 0;
					#if key1_Ctrl_id == KeyCtrl_UpBoard
					key1_param.keyState   = -2;
					mEventUpKeyTreat();	 
					#else
					mEventKeyTreat();
					#endif
				}
				
				if(key_get_level() != key1InitLevel && key1_param.keyDownTms > 0 && system_time - key1_param.keyDownTms > Key_longHold && key1_param.keyState != -3)
				{
					#if key1_Ctrl_id == KeyCtrl_UpBoard
					key1_param.keyState   = -3;
					mEventUpKeyTreat();	 
					#else
					mEventKeyTreat();
					#endif
				}
				#endif
	  }
}
#endif // #if Key1  > 0


// 接地检测
#ifndef UsingFreeRTOS
#if PE_ON > 0
void PEDetect(){
	  if(PE_get_level()){
			  // 高电平，就检测出了波形; 接地发生，恢复到0；低电平就不管。
			  //if(PE_occ_times < 253){
					 //PE_occ_times++; 
				//}		
			  // 检测到1就置0;
			  PE_occ_recode = 0;
		} else {
			  // 没有接地; 一直不接地，一直 = 0;
			  // 记录首次变0时间：可计算等待0的时间;
			  if(PE_occ_recode == 0){
            #if RTC_ON > 0
						getTimestampRTC();
						#endif
					  PE_occ_recode = system_time;
				}	
		}	
    #if PE_method == PE_method_ADC 		
		//printf(" %d/%d/%lld/%lld/%d ",PE_get_level(),ADC_Buffer.ADC_Item.ADCValue_PE,(system_time - PE_occ_recode),PE_occ_recode,(PE_ADC_Value + PE_VoltRest));
		#else
		//printf(" %d/%lld/%lld ",PE_get_level(),(system_time - PE_occ_recode),PE_occ_recode); 
		#endif
}
#endif
#endif // #ifndef UsingFreeRTOS 

#if CarD1_Protect > 0 && (CarD1_Detect_Tyte == CarD1_Detect_EINT)
// 车载二极管检测：外部中断
void CarD1_Eint_Fun(){
	  // 车载二极管中断发生：车载二极管正常就会发生中断。
	  if(EINT_ReadIntFlag(CarD1_EintLine)){			  
        CarD1Param.CarD1_MarkNum++;        
        if(CarD1Param.CarD1_MarkNum > 250){   
            carD1_detect_stop();
        }
				printf(" CarD1_Eint_Fun %d %d \r\n",CarD1Param.CarD1_MarkNum,EINT_ReadIntFlag(CarD1_EintLine));
        EINT_ClearIntFlag(CarD1_EintLine);
    }
}

#endif

// 活体检测
#if finger_sensor_type == Finger_HLK_ZW101_EINT  
void ZW101_Eint_Fun(){  	
	  if(EINT_ReadIntFlag(ZW101_EintLine)){ 
        // 检测到手指
			  if(fingerCtrl.finger_busy == 0){
			      fingerCtrl.finger_event_occ++; 
				}
        EINT_ClearIntFlag(ZW101_EintLine);   
    }
}
#endif

// 漏电检测
#if RCD_Detect == RCD_Detect_POLL
void LeakDetect(){
	  // 高电平	  
	  if(leakage_get_level()){
			  leakage_event_treat();
				if(leakData.leak_tms <= 252){
						leakData.leak_tms++;					  
				}				
		} 
}
#endif
#if RCD_Detect == RCD_Detect_EINT
void Leakage_Eint_Fun(){
	  if(EINT_ReadIntFlag(Leakage_EintLine)){ 
        // 检测到有漏电
			  leakage_event_treat();
				if(leakData.leak_tms <= 252){
						leakData.leak_tms++;
				}
        EINT_ClearIntFlag(Leakage_EintLine);
    }
}
#endif

#if CC_Detect_type > 0
// CC检测：CC_get_level() = 0 = 插枪; 1 = 未插枪
void CC_Detect(){
	  if(CC_get_level()){
        if(PlugIn_CC == 1){
				    // 拔枪动作：
						plug_out_action();
				} 
			  PlugIn_CC = 0;
		}else{
        if(PlugIn_CC == 0){
				    // 插枪动作：
						plug_in_action();
				}
			  PlugIn_CC = 1;
		}
		#ifdef SYS_DEBUG
		//printf(" CC_Detect  %d  ",PlugIn_CC);
		#endif
}
#endif

#if RlyAdh_Detect_type > 0
// 继电器粘连检测：长期检测不到0, 粘连。
void RlyAdh_Detect(){
	  if(adhesion_get_level() && cp_data.value_cope_id != CP_6){	
        // 检测到1就，就是粘连开始		  
			  if(rlyAdh_ToZero_tm == 0){ 
				    #if RTC_ON > 0
						getTimestampRTC();
						#endif
					  rlyAdh_ToZero_tm = system_time;  
				}
		} else {
			  rlyAdh_ToZero_tm = 0;
		}
}  //#if RlyAdh_Detect_type > 0
#endif





#endif
